package com.beamfield.controller.manage.sys;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.dictionary.Constant;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Message;
import com.beamfield.service.manage.sys.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Zephyr
 * @Description: 消息
 * @Date 2018/8/9
 */
@RequestMapping("/cms/message")
@Controller
public class MessageController extends BaseController {
    final private MessageService messageService;

    @Autowired
    public MessageController(MessageService messageService){
        this.messageService = messageService;
    }

    /**
     * 获取消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readMessageData")
    @ResponseBody
    public PageResult readMessageData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String titlecontent = request.getParameter("titlecontent");
        List<Message> attendMachineList = messageService.readMessageData(offset,limit,beamId,userId,titlecontent, null);
        long total = messageService.countMessageData(beamId, userId,titlecontent, null);
        return new PageResult(total, CommonUtils.parseLong(offset,0L),attendMachineList);
    }
    /**
     * 根据id获取消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readOneMessage")
    @ResponseBody
    public ApiResult readOneMessage(HttpServletRequest request){
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Message message = messageService.readOneMessage(id);
        return new ApiResult(message);
    }
    /**
     * 获取未读消息数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/countUnreadMessages",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult countUnreadMessages(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        long total = messageService.countUnreadMessages(beamId, userId, null);
        return new ApiResult(HttpResultEnum.SUCCESS.getCode(), HttpResultEnum.SUCCESS.getMessage(), total);
    }
    /**
     * 更新消息状态
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateMessageStatus",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateMessageStatus(HttpServletRequest request){
        String id = request.getParameter("id");
        Long userId = super.getUserId(request);
        try {
            int result = messageService.updateMessageStatus(id, userId, Constant.MESSAGE_READ);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new ApiResult(HttpResultEnum.SUCCESS.getCode(), HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 根据获取所有消息
     * @return
     */
    @RequestMapping(value = "/view")
    public ModelAndView view(){
        return new ModelAndView();
    }
}
